{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 3\n",
    "# Predicting Sports Winners with Decision Trees"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "data_filename = \"basketball.csv\"\n",
    "dataset = pd.read_csv(data_filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Start (ET)</th>\n",
       "      <th>Visitor/Neutral</th>\n",
       "      <th>PTS</th>\n",
       "      <th>Home/Neutral</th>\n",
       "      <th>PTS.1</th>\n",
       "      <th>Unnamed: 6</th>\n",
       "      <th>Unnamed: 7</th>\n",
       "      <th>Attend.</th>\n",
       "      <th>Notes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Tue Oct 17 2017</td>\n",
       "      <td>8:01p</td>\n",
       "      <td>Boston Celtics</td>\n",
       "      <td>99</td>\n",
       "      <td>Cleveland Cavaliers</td>\n",
       "      <td>102</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20562.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Tue Oct 17 2017</td>\n",
       "      <td>10:30p</td>\n",
       "      <td>Houston Rockets</td>\n",
       "      <td>122</td>\n",
       "      <td>Golden State Warriors</td>\n",
       "      <td>121</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>19596.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Wed Oct 18 2017</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Charlotte Hornets</td>\n",
       "      <td>90</td>\n",
       "      <td>Detroit Pistons</td>\n",
       "      <td>102</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20491.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Wed Oct 18 2017</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Brooklyn Nets</td>\n",
       "      <td>131</td>\n",
       "      <td>Indiana Pacers</td>\n",
       "      <td>140</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>15008.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Wed Oct 18 2017</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Miami Heat</td>\n",
       "      <td>109</td>\n",
       "      <td>Orlando Magic</td>\n",
       "      <td>116</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18846.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Wed Oct 18 2017</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Philadelphia 76ers</td>\n",
       "      <td>115</td>\n",
       "      <td>Washington Wizards</td>\n",
       "      <td>120</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20356.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Date Start (ET)     Visitor/Neutral  PTS           Home/Neutral  \\\n",
       "0  Tue Oct 17 2017      8:01p      Boston Celtics   99    Cleveland Cavaliers   \n",
       "1  Tue Oct 17 2017     10:30p     Houston Rockets  122  Golden State Warriors   \n",
       "2  Wed Oct 18 2017      7:00p   Charlotte Hornets   90        Detroit Pistons   \n",
       "3  Wed Oct 18 2017      7:00p       Brooklyn Nets  131         Indiana Pacers   \n",
       "4  Wed Oct 18 2017      7:00p          Miami Heat  109          Orlando Magic   \n",
       "5  Wed Oct 18 2017      7:00p  Philadelphia 76ers  115     Washington Wizards   \n",
       "\n",
       "   PTS.1 Unnamed: 6 Unnamed: 7  Attend.  Notes  \n",
       "0    102  Box Score        NaN  20562.0    NaN  \n",
       "1    121  Box Score        NaN  19596.0    NaN  \n",
       "2    102  Box Score        NaN  20491.0    NaN  \n",
       "3    140  Box Score        NaN  15008.0    NaN  \n",
       "4    116  Box Score        NaN  18846.0    NaN  \n",
       "5    120  Box Score        NaN  20356.0    NaN  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.head(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import pandas as pd\n",
    "# fields = [ \"Date\", \"Start (ET)\", \"Visitor/Neutral\", \"PTS\", \"Home/Neutral\", \"PTS.1\"]\n",
    "\n",
    "# dataset = pd.read_csv('basketball.csv', parse_dates=[\"Date\"], skipinitialspace=True, usecols=fields)\n",
    "# dataset.columns = [\"Date\", \"Start (ET)\", \"Visitor Team\", \"VisitorPts\", \"Home Team\", \"HomePts\"]\n",
    "# dataset.head(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Start (ET)</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>VisitorPts</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>HomePts</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2017-10-17</td>\n",
       "      <td>8:01p</td>\n",
       "      <td>Boston Celtics</td>\n",
       "      <td>99</td>\n",
       "      <td>Cleveland Cavaliers</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017-10-17</td>\n",
       "      <td>10:30p</td>\n",
       "      <td>Houston Rockets</td>\n",
       "      <td>122</td>\n",
       "      <td>Golden State Warriors</td>\n",
       "      <td>121</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Charlotte Hornets</td>\n",
       "      <td>90</td>\n",
       "      <td>Detroit Pistons</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Brooklyn Nets</td>\n",
       "      <td>131</td>\n",
       "      <td>Indiana Pacers</td>\n",
       "      <td>140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Miami Heat</td>\n",
       "      <td>109</td>\n",
       "      <td>Orlando Magic</td>\n",
       "      <td>116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Philadelphia 76ers</td>\n",
       "      <td>115</td>\n",
       "      <td>Washington Wizards</td>\n",
       "      <td>120</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date Start (ET)        Visitor Team  VisitorPts  \\\n",
       "0 2017-10-17      8:01p      Boston Celtics          99   \n",
       "1 2017-10-17     10:30p     Houston Rockets         122   \n",
       "2 2017-10-18      7:00p   Charlotte Hornets          90   \n",
       "3 2017-10-18      7:00p       Brooklyn Nets         131   \n",
       "4 2017-10-18      7:00p          Miami Heat         109   \n",
       "5 2017-10-18      7:00p  Philadelphia 76ers         115   \n",
       "\n",
       "               Home Team  HomePts  \n",
       "0    Cleveland Cavaliers      102  \n",
       "1  Golden State Warriors      121  \n",
       "2        Detroit Pistons      102  \n",
       "3         Indiana Pacers      140  \n",
       "4          Orlando Magic      116  \n",
       "5     Washington Wizards      120  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "dataset = pd.read_csv('basketball.csv', parse_dates=[\"Date\"], skipinitialspace=True, usecols=range(6))\n",
    "dataset.columns = [\"Date\", \"Start (ET)\", \"Visitor Team\", \"VisitorPts\", \"Home Team\", \"HomePts\"]\n",
    "dataset.head(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = pd.read_csv(data_filename, parse_dates=[\"Date\"])\n",
    "\n",
    "dataset.columns = [\"Date\", \"Start (ET)\", \"Visitor Team\", \"VisitorPts\", \"Home Team\", \"HomePts\", \"OT?\", \"Score Type\", \"Attend\", \"Notes\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Start (ET)</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>VisitorPts</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>HomePts</th>\n",
       "      <th>OT?</th>\n",
       "      <th>Score Type</th>\n",
       "      <th>Attend</th>\n",
       "      <th>Notes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2017-10-17</td>\n",
       "      <td>8:01p</td>\n",
       "      <td>Boston Celtics</td>\n",
       "      <td>99</td>\n",
       "      <td>Cleveland Cavaliers</td>\n",
       "      <td>102</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20562.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017-10-17</td>\n",
       "      <td>10:30p</td>\n",
       "      <td>Houston Rockets</td>\n",
       "      <td>122</td>\n",
       "      <td>Golden State Warriors</td>\n",
       "      <td>121</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>19596.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Charlotte Hornets</td>\n",
       "      <td>90</td>\n",
       "      <td>Detroit Pistons</td>\n",
       "      <td>102</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20491.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Brooklyn Nets</td>\n",
       "      <td>131</td>\n",
       "      <td>Indiana Pacers</td>\n",
       "      <td>140</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>15008.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Miami Heat</td>\n",
       "      <td>109</td>\n",
       "      <td>Orlando Magic</td>\n",
       "      <td>116</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18846.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Philadelphia 76ers</td>\n",
       "      <td>115</td>\n",
       "      <td>Washington Wizards</td>\n",
       "      <td>120</td>\n",
       "      <td>Box Score</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20356.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date Start (ET)        Visitor Team  VisitorPts  \\\n",
       "0 2017-10-17      8:01p      Boston Celtics          99   \n",
       "1 2017-10-17     10:30p     Houston Rockets         122   \n",
       "2 2017-10-18      7:00p   Charlotte Hornets          90   \n",
       "3 2017-10-18      7:00p       Brooklyn Nets         131   \n",
       "4 2017-10-18      7:00p          Miami Heat         109   \n",
       "5 2017-10-18      7:00p  Philadelphia 76ers         115   \n",
       "\n",
       "               Home Team  HomePts        OT? Score Type   Attend  Notes  \n",
       "0    Cleveland Cavaliers      102  Box Score        NaN  20562.0    NaN  \n",
       "1  Golden State Warriors      121  Box Score        NaN  19596.0    NaN  \n",
       "2        Detroit Pistons      102  Box Score        NaN  20491.0    NaN  \n",
       "3         Indiana Pacers      140  Box Score        NaN  15008.0    NaN  \n",
       "4          Orlando Magic      116  Box Score        NaN  18846.0    NaN  \n",
       "5     Washington Wizards      120  Box Score        NaN  20356.0    NaN  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.head(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Start (ET)</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>VisitorPts</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>HomePts</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2017-10-17</td>\n",
       "      <td>8:01p</td>\n",
       "      <td>Boston Celtics</td>\n",
       "      <td>99</td>\n",
       "      <td>Cleveland Cavaliers</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017-10-17</td>\n",
       "      <td>10:30p</td>\n",
       "      <td>Houston Rockets</td>\n",
       "      <td>122</td>\n",
       "      <td>Golden State Warriors</td>\n",
       "      <td>121</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Charlotte Hornets</td>\n",
       "      <td>90</td>\n",
       "      <td>Detroit Pistons</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Brooklyn Nets</td>\n",
       "      <td>131</td>\n",
       "      <td>Indiana Pacers</td>\n",
       "      <td>140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Miami Heat</td>\n",
       "      <td>109</td>\n",
       "      <td>Orlando Magic</td>\n",
       "      <td>116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Philadelphia 76ers</td>\n",
       "      <td>115</td>\n",
       "      <td>Washington Wizards</td>\n",
       "      <td>120</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date Start (ET)        Visitor Team  VisitorPts  \\\n",
       "0 2017-10-17      8:01p      Boston Celtics          99   \n",
       "1 2017-10-17     10:30p     Houston Rockets         122   \n",
       "2 2017-10-18      7:00p   Charlotte Hornets          90   \n",
       "3 2017-10-18      7:00p       Brooklyn Nets         131   \n",
       "4 2017-10-18      7:00p          Miami Heat         109   \n",
       "5 2017-10-18      7:00p  Philadelphia 76ers         115   \n",
       "\n",
       "               Home Team  HomePts  \n",
       "0    Cleveland Cavaliers      102  \n",
       "1  Golden State Warriors      121  \n",
       "2        Detroit Pistons      102  \n",
       "3         Indiana Pacers      140  \n",
       "4          Orlando Magic      116  \n",
       "5     Washington Wizards      120  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.drop([\"OT?\", \"Score Type\", \"Attend\", \"Notes\"], axis = 1, inplace = True) \n",
    "dataset.head(6)\n",
    "# print(dataset.dtypes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset[\"HomeWin\"] = dataset[\"VisitorPts\"] < dataset[\"HomePts\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Start (ET)</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>VisitorPts</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>HomePts</th>\n",
       "      <th>HomeWin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2017-10-17</td>\n",
       "      <td>8:01p</td>\n",
       "      <td>Boston Celtics</td>\n",
       "      <td>99</td>\n",
       "      <td>Cleveland Cavaliers</td>\n",
       "      <td>102</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017-10-17</td>\n",
       "      <td>10:30p</td>\n",
       "      <td>Houston Rockets</td>\n",
       "      <td>122</td>\n",
       "      <td>Golden State Warriors</td>\n",
       "      <td>121</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Charlotte Hornets</td>\n",
       "      <td>90</td>\n",
       "      <td>Detroit Pistons</td>\n",
       "      <td>102</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Brooklyn Nets</td>\n",
       "      <td>131</td>\n",
       "      <td>Indiana Pacers</td>\n",
       "      <td>140</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Miami Heat</td>\n",
       "      <td>109</td>\n",
       "      <td>Orlando Magic</td>\n",
       "      <td>116</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Philadelphia 76ers</td>\n",
       "      <td>115</td>\n",
       "      <td>Washington Wizards</td>\n",
       "      <td>120</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date Start (ET)        Visitor Team  VisitorPts  \\\n",
       "0 2017-10-17      8:01p      Boston Celtics          99   \n",
       "1 2017-10-17     10:30p     Houston Rockets         122   \n",
       "2 2017-10-18      7:00p   Charlotte Hornets          90   \n",
       "3 2017-10-18      7:00p       Brooklyn Nets         131   \n",
       "4 2017-10-18      7:00p          Miami Heat         109   \n",
       "5 2017-10-18      7:00p  Philadelphia 76ers         115   \n",
       "\n",
       "               Home Team  HomePts  HomeWin  \n",
       "0    Cleveland Cavaliers      102     True  \n",
       "1  Golden State Warriors      121    False  \n",
       "2        Detroit Pistons      102     True  \n",
       "3         Indiana Pacers      140     True  \n",
       "4          Orlando Magic      116     True  \n",
       "5     Washington Wizards      120     True  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.head(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_true = dataset[\"HomeWin\"].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5888501742160279"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset[\"HomeWin\"].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import defaultdict #create a dictionarty\n",
    "won_last = defaultdict(int)\n",
    "# print(won_last)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset[\"HomeLastWin\"] = 0\n",
    "dataset[\"VisitorLastWin\"] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "for index, row in dataset.iterrows():\n",
    "    home_team = row[\"Home Team\"]\n",
    "    visitor_team = row[\"Visitor Team\"]\n",
    "    row[\"HomeLastWin\"] = won_last[home_team]\n",
    "    dataset.at[index, \"HomeLastWin\"] = won_last[home_team]\n",
    "    dataset.at[index, \"VisitorLastWin\"] = won_last[visitor_team]\n",
    "    \n",
    "#     dataset = pd.at(index, \"HomeLastWin\", won_last[home_team])\n",
    "#     dataset = pd.at(index, \"VisitorLastWin\", won_last[visitor_team])\n",
    "    \n",
    "    won_last[home_team] = int(row[\"HomeWin\"])\n",
    "    won_last[visitor_team] = 1 - int(row[\"HomeWin\"])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Start (ET)</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>VisitorPts</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>HomePts</th>\n",
       "      <th>HomeWin</th>\n",
       "      <th>HomeLastWin</th>\n",
       "      <th>VisitorLastWin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2017-10-17</td>\n",
       "      <td>8:01p</td>\n",
       "      <td>Boston Celtics</td>\n",
       "      <td>99</td>\n",
       "      <td>Cleveland Cavaliers</td>\n",
       "      <td>102</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017-10-17</td>\n",
       "      <td>10:30p</td>\n",
       "      <td>Houston Rockets</td>\n",
       "      <td>122</td>\n",
       "      <td>Golden State Warriors</td>\n",
       "      <td>121</td>\n",
       "      <td>False</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Charlotte Hornets</td>\n",
       "      <td>90</td>\n",
       "      <td>Detroit Pistons</td>\n",
       "      <td>102</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Brooklyn Nets</td>\n",
       "      <td>131</td>\n",
       "      <td>Indiana Pacers</td>\n",
       "      <td>140</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Miami Heat</td>\n",
       "      <td>109</td>\n",
       "      <td>Orlando Magic</td>\n",
       "      <td>116</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2017-10-18</td>\n",
       "      <td>7:00p</td>\n",
       "      <td>Philadelphia 76ers</td>\n",
       "      <td>115</td>\n",
       "      <td>Washington Wizards</td>\n",
       "      <td>120</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date Start (ET)        Visitor Team  VisitorPts  \\\n",
       "0 2017-10-17      8:01p      Boston Celtics          99   \n",
       "1 2017-10-17     10:30p     Houston Rockets         122   \n",
       "2 2017-10-18      7:00p   Charlotte Hornets          90   \n",
       "3 2017-10-18      7:00p       Brooklyn Nets         131   \n",
       "4 2017-10-18      7:00p          Miami Heat         109   \n",
       "5 2017-10-18      7:00p  Philadelphia 76ers         115   \n",
       "\n",
       "               Home Team  HomePts  HomeWin  HomeLastWin  VisitorLastWin  \n",
       "0    Cleveland Cavaliers      102     True            0               0  \n",
       "1  Golden State Warriors      121    False            0               0  \n",
       "2        Detroit Pistons      102     True            0               0  \n",
       "3         Indiana Pacers      140     True            0               0  \n",
       "4          Orlando Magic      116     True            0               0  \n",
       "5     Washington Wizards      120     True            0               0  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.head(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Start (ET)</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>VisitorPts</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>HomePts</th>\n",
       "      <th>HomeWin</th>\n",
       "      <th>HomeLastWin</th>\n",
       "      <th>VisitorLastWin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [Date, Start (ET), Visitor Team, VisitorPts, Home Team, HomePts, HomeWin, HomeLastWin, VisitorLastWin]\n",
       "Index: []"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.iloc[1000:1005]\n",
    "# dataset.loc[1000:1005]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_previouswins = dataset[[\"HomeLastWin\", \"VisitorLastWin\"]].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "# clf = DecisionTreeClassifier() #classifier\n",
    "clf = DecisionTreeClassifier(random_state=14)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import cross_val_score\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 58.9%\n"
     ]
    }
   ],
   "source": [
    "scores = cross_val_score(clf, X_previouswins, y_true,\n",
    "scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "standings_filename = os.path.join(\"standings.csv\")\n",
    "\n",
    "standings = pd.read_csv(standings_filename, skiprows=1, error_bad_lines=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Rk</th>\n",
       "      <th>Team</th>\n",
       "      <th>Overall</th>\n",
       "      <th>Home</th>\n",
       "      <th>Road</th>\n",
       "      <th>E</th>\n",
       "      <th>W</th>\n",
       "      <th>A</th>\n",
       "      <th>C</th>\n",
       "      <th>SE</th>\n",
       "      <th>...</th>\n",
       "      <th>Post</th>\n",
       "      <th>???3</th>\n",
       "      <th>???10</th>\n",
       "      <th>Oct</th>\n",
       "      <th>Nov</th>\n",
       "      <th>Dec</th>\n",
       "      <th>Jan</th>\n",
       "      <th>Feb</th>\n",
       "      <th>Mar</th>\n",
       "      <th>Apr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Houston Rockets</td>\n",
       "      <td>65-17</td>\n",
       "      <td>34-7</td>\n",
       "      <td>31-Oct</td>\n",
       "      <td>24-Jun</td>\n",
       "      <td>41-11</td>\n",
       "      <td>06-Apr</td>\n",
       "      <td>09-Jan</td>\n",
       "      <td>09-Jan</td>\n",
       "      <td>...</td>\n",
       "      <td>21-Apr</td>\n",
       "      <td>05-Mar</td>\n",
       "      <td>38-8</td>\n",
       "      <td>05-Mar</td>\n",
       "      <td>12-Jan</td>\n",
       "      <td>09-May</td>\n",
       "      <td>10-Apr</td>\n",
       "      <td>12-0</td>\n",
       "      <td>14-Jan</td>\n",
       "      <td>03-Mar</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Toronto Raptors</td>\n",
       "      <td>59-23</td>\n",
       "      <td>34-7</td>\n",
       "      <td>25-16</td>\n",
       "      <td>40-12</td>\n",
       "      <td>19-Nov</td>\n",
       "      <td>12-Apr</td>\n",
       "      <td>14-Apr</td>\n",
       "      <td>14-Apr</td>\n",
       "      <td>...</td>\n",
       "      <td>18-Jul</td>\n",
       "      <td>05-Jul</td>\n",
       "      <td>33-5</td>\n",
       "      <td>04-Feb</td>\n",
       "      <td>09-May</td>\n",
       "      <td>11-Mar</td>\n",
       "      <td>10-May</td>\n",
       "      <td>09-Feb</td>\n",
       "      <td>12-Apr</td>\n",
       "      <td>04-Feb</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Golden State Warriors</td>\n",
       "      <td>58-24</td>\n",
       "      <td>29-Dec</td>\n",
       "      <td>29-Dec</td>\n",
       "      <td>24-Jun</td>\n",
       "      <td>34-18</td>\n",
       "      <td>09-Jan</td>\n",
       "      <td>06-Apr</td>\n",
       "      <td>09-Jan</td>\n",
       "      <td>...</td>\n",
       "      <td>14-Oct</td>\n",
       "      <td>05-Jan</td>\n",
       "      <td>38-13</td>\n",
       "      <td>05-Mar</td>\n",
       "      <td>11-Mar</td>\n",
       "      <td>13-Feb</td>\n",
       "      <td>11-Mar</td>\n",
       "      <td>08-Mar</td>\n",
       "      <td>07-Jul</td>\n",
       "      <td>03-Mar</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Boston Celtics</td>\n",
       "      <td>55-27</td>\n",
       "      <td>27-14</td>\n",
       "      <td>28-13</td>\n",
       "      <td>33-19</td>\n",
       "      <td>22-Aug</td>\n",
       "      <td>12-Apr</td>\n",
       "      <td>10-Aug</td>\n",
       "      <td>11-Jul</td>\n",
       "      <td>...</td>\n",
       "      <td>15-Aug</td>\n",
       "      <td>11-Aug</td>\n",
       "      <td>25-Sep</td>\n",
       "      <td>05-Feb</td>\n",
       "      <td>14-Feb</td>\n",
       "      <td>11-Jun</td>\n",
       "      <td>07-May</td>\n",
       "      <td>07-Apr</td>\n",
       "      <td>09-Apr</td>\n",
       "      <td>02-Apr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Philadelphia 76ers</td>\n",
       "      <td>52-30</td>\n",
       "      <td>30-Nov</td>\n",
       "      <td>22-19</td>\n",
       "      <td>34-18</td>\n",
       "      <td>18-Dec</td>\n",
       "      <td>09-Jul</td>\n",
       "      <td>11-Jul</td>\n",
       "      <td>14-Apr</td>\n",
       "      <td>...</td>\n",
       "      <td>22-May</td>\n",
       "      <td>04-Jul</td>\n",
       "      <td>31-11</td>\n",
       "      <td>03-Apr</td>\n",
       "      <td>09-May</td>\n",
       "      <td>05-Oct</td>\n",
       "      <td>07-May</td>\n",
       "      <td>08-Mar</td>\n",
       "      <td>13-Mar</td>\n",
       "      <td>7-0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Rk                   Team Overall    Home    Road       E       W       A  \\\n",
       "0   1        Houston Rockets   65-17    34-7  31-Oct  24-Jun   41-11  06-Apr   \n",
       "1   2        Toronto Raptors   59-23    34-7   25-16   40-12  19-Nov  12-Apr   \n",
       "2   3  Golden State Warriors   58-24  29-Dec  29-Dec  24-Jun   34-18  09-Jan   \n",
       "3   4         Boston Celtics   55-27   27-14   28-13   33-19  22-Aug  12-Apr   \n",
       "4   5     Philadelphia 76ers   52-30  30-Nov   22-19   34-18  18-Dec  09-Jul   \n",
       "\n",
       "        C      SE  ...    Post    ???3   ???10     Oct     Nov     Dec  \\\n",
       "0  09-Jan  09-Jan  ...  21-Apr  05-Mar    38-8  05-Mar  12-Jan  09-May   \n",
       "1  14-Apr  14-Apr  ...  18-Jul  05-Jul    33-5  04-Feb  09-May  11-Mar   \n",
       "2  06-Apr  09-Jan  ...  14-Oct  05-Jan   38-13  05-Mar  11-Mar  13-Feb   \n",
       "3  10-Aug  11-Jul  ...  15-Aug  11-Aug  25-Sep  05-Feb  14-Feb  11-Jun   \n",
       "4  11-Jul  14-Apr  ...  22-May  04-Jul   31-11  03-Apr  09-May  05-Oct   \n",
       "\n",
       "      Jan     Feb     Mar     Apr  \n",
       "0  10-Apr    12-0  14-Jan  03-Mar  \n",
       "1  10-May  09-Feb  12-Apr  04-Feb  \n",
       "2  11-Mar  08-Mar  07-Jul  03-Mar  \n",
       "3  07-May  07-Apr  09-Apr  02-Apr  \n",
       "4  07-May  08-Mar  13-Mar     7-0  \n",
       "\n",
       "[5 rows x 24 columns]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "standings.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset[\"HomeTeamRanksHigher\"] = 0\n",
    "for index, row in dataset.iterrows():\n",
    "    home_team = row[\"Home Team\"]\n",
    "    visitor_team = row[\"Visitor Team\"]\n",
    "    home_rank = standings[standings[\"Team\"] == home_team][\"Rk\"].values[0]\n",
    "    visitor_rank = standings[standings[\"Team\"] == visitor_team][\"Rk\"].values[0]\n",
    "    dataset.at[index, \"HomeTeamRanksHigher\"] = int(home_rank < visitor_rank)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_homehigher = dataset[[ \"HomeTeamRanksHigher\", \"HomeLastWin\", \"VisitorLastWin\",]].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 61.3%\n"
     ]
    }
   ],
   "source": [
    "clf = DecisionTreeClassifier(random_state=14, criterion=\"entropy\")\n",
    "\n",
    "scores = cross_val_score(clf, X_homehigher, y_true, scoring='accuracy')\n",
    "\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "last_match_winner = defaultdict(int)\n",
    "# print(last_match_winner)\n",
    "dataset[\"HomeTeamWonLast\"] = 0\n",
    "\n",
    "for index, row in dataset.iterrows():\n",
    "    home_team = row[\"Home Team\"]\n",
    "    visitor_team = row[\"Visitor Team\"]\n",
    "    teams = tuple(sorted([home_team, visitor_team]))  # Sort for a consistent ordering in last_match_winner\n",
    "#     print(last_match_winner)\n",
    "    # Set in the row, who won the last encounter\n",
    "    home_team_won_last = 1 if last_match_winner[teams] == row[\"Home Team\"] else 0\n",
    "    dataset.at[index, \"HomeTeamWonLast\"] = home_team_won_last\n",
    "    # Who won this one?\n",
    "    winner = row[\"Home Team\"] if row[\"HomeWin\"] else row[\"Visitor Team\"]\n",
    "    last_match_winner[teams] = winner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Start (ET)</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>VisitorPts</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>HomePts</th>\n",
       "      <th>HomeWin</th>\n",
       "      <th>HomeLastWin</th>\n",
       "      <th>VisitorLastWin</th>\n",
       "      <th>HomeTeamRanksHigher</th>\n",
       "      <th>HomeTeamWonLast</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [Date, Start (ET), Visitor Team, VisitorPts, Home Team, HomePts, HomeWin, HomeLastWin, VisitorLastWin, HomeTeamRanksHigher, HomeTeamWonLast]\n",
       "Index: []"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.iloc[400:450]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 57.8%\n"
     ]
    }
   ],
   "source": [
    "X_lastwinner = dataset[[ \"HomeTeamWonLast\", \"HomeTeamRanksHigher\", \"HomeLastWin\", \"VisitorLastWin\",]].values\n",
    "clf = DecisionTreeClassifier(random_state=14, criterion=\"entropy\")\n",
    "\n",
    "scores = cross_val_score(clf, X_lastwinner, y_true, scoring='accuracy')\n",
    "\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Start (ET)</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>VisitorPts</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>HomePts</th>\n",
       "      <th>HomeWin</th>\n",
       "      <th>HomeLastWin</th>\n",
       "      <th>VisitorLastWin</th>\n",
       "      <th>HomeTeamRanksHigher</th>\n",
       "      <th>HomeTeamWonLast</th>\n",
       "      <th>TeamWonLastf</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [Date, Start (ET), Visitor Team, VisitorPts, Home Team, HomePts, HomeWin, HomeLastWin, VisitorLastWin, HomeTeamRanksHigher, HomeTeamWonLast, TeamWonLastf]\n",
       "Index: []"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "last_match_winnerf = defaultdict(int)\n",
    "# print(last_match_winner)\n",
    "dataset[\"TeamWonLastf\"] = 0\n",
    "home_team_won_lastf = 0\n",
    "\n",
    "for index, row in dataset.iterrows():\n",
    "    home_team = row[\"Home Team\"]\n",
    "    visitor_team = row[\"Visitor Team\"]\n",
    "    teams = tuple(sorted([home_team, visitor_team]))  # Sort for a consistent ordering in last_match_winner\n",
    "#     print(last_match_winnerf)\n",
    "    # Set in the row, who won the last encounter\n",
    "    temp = 1 if last_match_winner[teams] == row[\"Home Team\"] else 0\n",
    "    home_team_won_lastf += temp\n",
    "    if home_team_won_lastf == 5:\n",
    "        home_team_won_lastf = 0\n",
    "        dataset.at[index, \"TeamWonLastf\"] = home_team_won_last\n",
    "        # Who won this one?\n",
    "        winner = row[\"Home Team\"] if row[\"HomeWin\"] else row[\"Visitor Team\"]\n",
    "        last_match_winnerf[teams] = winner\n",
    "dataset.iloc[400:450]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 60.6%\n"
     ]
    }
   ],
   "source": [
    "X_lastwinnerf = dataset[[ \"TeamWonLastf\", \"HomeTeamRanksHigher\", \"HomeLastWin\", \"VisitorLastWin\",]].values\n",
    "clf = DecisionTreeClassifier(random_state=14, criterion=\"entropy\")\n",
    "\n",
    "scores = cross_val_score(clf, X_lastwinnerf, y_true, scoring='accuracy')\n",
    "\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 55.0%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.preprocessing import LabelEncoder\n",
    "encoding = LabelEncoder()\n",
    "encoding.fit(dataset[\"Home Team\"].values)\n",
    "home_teams = encoding.transform(dataset[\"Home Team\"].values)\n",
    "visitor_teams = encoding.transform(dataset[\"Visitor Team\"].values)\n",
    "X_teams = np.vstack([home_teams, visitor_teams]).T\n",
    "\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "onehot = OneHotEncoder()\n",
    "X_teams = onehot.fit_transform(X_teams).todense()\n",
    "\n",
    "clf = DecisionTreeClassifier(random_state=14)\n",
    "scores = cross_val_score(clf, X_teams, y_true, scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 54.7%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "clf = RandomForestClassifier(random_state=14)\n",
    "scores = cross_val_score(clf, X_teams, y_true, scoring='accuracy')\n",
    "# scores = cross_val_score(clf, X_lastwinner, y_true, scoring='accuracy')\n",
    "# scores = cross_val_score(clf, X_homehigher, y_true, scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 58.9%\n"
     ]
    }
   ],
   "source": [
    "# X_all = np.hstack([X_lastwinner, X_teams])\n",
    "X_all = np.hstack([X_homehigher, X_teams])\n",
    "clf = RandomForestClassifier(random_state=14)\n",
    "scores = cross_val_score(clf, X_all, y_true, scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 59.2%\n"
     ]
    }
   ],
   "source": [
    "# X_all = np.hstack([X_lastwinner, X_teams])\n",
    "X_all = np.hstack([X_homehigher, X_teams])\n",
    "clf = RandomForestClassifier(random_state=14, n_estimators=500)\n",
    "scores = cross_val_score(clf, X_all, y_true, scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 60.6%\n"
     ]
    }
   ],
   "source": [
    "# from sklearn.grid_search import GridSearchCV\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "parameter_space = {\n",
    "    \"max_features\": [2, 10, 'auto'],\n",
    "#     \"n_estimators\": [100, 200],\n",
    "    \"n_estimators\": [200],\n",
    "    \"criterion\": [\"gini\", \"entropy\"],\n",
    "#     \"min_samples_leaf\": [2, 4, 6],\n",
    "    \"min_samples_leaf\": [4],\n",
    "}\n",
    "clf = RandomForestClassifier(random_state=14)\n",
    "grid = GridSearchCV(clf, parameter_space)\n",
    "grid.fit(X_all, y_true)\n",
    "print(\"Accuracy: {0:.1f}%\".format(grid.best_score_ * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RandomForestClassifier(criterion='entropy', max_features=10, min_samples_leaf=4,\n",
      "                       n_estimators=200, random_state=14)\n"
     ]
    }
   ],
   "source": [
    "print(grid.best_estimator_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
